﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Dict.Post
{
    public partial class FrmPostDetail : Form
    {
        string strSQL;
        SqlConnection cnSQL;
        SqlCommand cmSQL;
        SqlDataReader drSQL;

        public FrmPostDetail()
        {
            InitializeComponent();
        }

        private void FrmPostDetail_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tIMSDataSet.dtPost' table. You can move, or remove it, as needed.
            this.dtPostTableAdapter1.Fill(this.tIMSDataSet.dtPost);
            //this.dtPostTableAdapter.Fill(this.dBTims01DataSet.dtPost);
            if (globals.UpdKey == 0)
            {
                this.Text = "Добавить должность";
            }
            else
            {
                if (globals.UpdKey == 1)
                {
                    this.Text = "Изменить должность";
                }
                else
                {
                    this.Text = "Вы уверены что хотите удалить должность";
                    this.toolStripButtonSave.Text = "Удалить";
                }

                strSQL = "Select * from dtPost where CodeID=" + Convert.ToString(globals.pCodeID);
                cnSQL = new SqlConnection(globals.APPCONNSTRING);
                cnSQL.Open();
                cmSQL = new SqlCommand(strSQL, cnSQL);
                drSQL = cmSQL.ExecuteReader();
                while (drSQL.Read())
                {
                    this.textBoxhName.Text = Convert.ToString(drSQL["hName"]);
                    this.comboBoxmgrid.SelectedValue = drSQL["mgrid"];
                }
                drSQL.Close();
                cnSQL.Close();
                cmSQL.Dispose();
                cnSQL.Dispose();
            }
        }

        private void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButtonSave_Click_1(object sender, EventArgs e)
        {
            int vmgrid = Convert.ToInt32(this.comboBoxmgrid.SelectedValue);
            if (globals.UpdKey == 0)
            {
                SqlCommand cmSQL = new SqlCommand();
                cmSQL.Connection = new SqlConnection(globals.APPCONNSTRING);
                cmSQL.CommandType = CommandType.StoredProcedure;
                cmSQL.CommandText = "tsp_dtPost";

                cmSQL.Parameters.Add("Action", SqlDbType.TinyInt).Value = globals.UpdKey;

                SqlParameter OutPutParCodeID = new SqlParameter("CodeID", SqlDbType.Int);
                OutPutParCodeID.Direction = ParameterDirection.Output;

                cmSQL.Parameters.Add(OutPutParCodeID);
                cmSQL.Parameters.Add("hName", SqlDbType.NVarChar).Value = this.textBoxhName.Text;
                if (vmgrid != 0)
                {
                    cmSQL.Parameters.Add("mgrid", SqlDbType.Int).Value = this.comboBoxmgrid.SelectedValue;
                }
                else
                {
                    cmSQL.Parameters.Add("mgrid", SqlDbType.Int).Value = DBNull.Value;
                }
                try
                {
                    cmSQL.Connection.Open();
                    cmSQL.ExecuteNonQuery();
                }
                catch (SqlException e1)
                {
                    //string ErrMsg = ErrFinder1.GetErrMessage(e1.GetType().Name, e1.ErrorCode.ToString(), e1.Message);
                    //System.Windows.Forms.MessageBox.Show(ErrMsg, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    //return false;
                    MessageBox.Show(e1.Message +Convert.ToString(globals.UpdKey)+Convert.ToString( this.comboBoxmgrid.SelectedValue));
                }
                finally
                {
                    this.Close();
                }
            }
            else
            {
                SqlCommand cmSQL = new SqlCommand();
                cmSQL.Connection = new SqlConnection(globals.APPCONNSTRING);
                cmSQL.CommandType = CommandType.StoredProcedure;
                cmSQL.CommandText = "tsp_dtPost";

                cmSQL.Parameters.Add("Action", SqlDbType.TinyInt).Value = globals.UpdKey;

                SqlParameter OutPutParCodeID = new SqlParameter("CodeID", SqlDbType.Int);
                OutPutParCodeID.Direction = ParameterDirection.InputOutput;
                OutPutParCodeID.Value = globals.pCodeID;
                cmSQL.Parameters.Add(OutPutParCodeID);

                cmSQL.Parameters.Add("hName", SqlDbType.NVarChar).Value = this.textBoxhName.Text;
                if (vmgrid != 0)
                {
                    cmSQL.Parameters.Add("mgrid", SqlDbType.Int).Value = this.comboBoxmgrid.SelectedValue;
                }
                else
                {
                    cmSQL.Parameters.Add("mgrid", SqlDbType.Int).Value = DBNull.Value;
                }
                try
                {
                    cmSQL.Connection.Open();
                    cmSQL.ExecuteNonQuery();
                }
                catch (SqlException e1)
                {
                    //string ErrMsg = ErrFinder1.GetErrMessage(e1.GetType().Name, e1.ErrorCode.ToString(), e1.Message);
                    //System.Windows.Forms.MessageBox.Show(ErrMsg, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    //return false;
                    MessageBox.Show(e1.Message);
                }
                finally
                {
                    this.Close();
                }
            }
        }
    }
}
